﻿
var mingAuthCallBack = function (data) {
    //console.log(data);
    if (data != 'null') {

        data = $.parseJSON(data);
        $("#idNameMing").html(data.username);
        $("#divLoginInfo").show();
        $("#divLogin").hide();
        removeSignOutVietIdStatus();
        reloadIFrame();
    }
};

function setSignOutVietIdStatus(value) {
    setCookie("soha_IsSignOutVietId", value, 30);
}

function removeSignOutVietIdStatus() {
    setCookie("soha_IsSignOutVietId", "", -1);
}

function getSignOutVietIdStatus() {
    return getCookie("soha_IsSignOutVietId");
}

var reloadIFrame = function () {
    if ($('.mingid_comment_iframe').length > 0) {
        $('.mingid_comment_iframe').each(function () {
            $(this).attr('src', $(this).attr('src'));
        });
    }
    return false;
};

var MingNotify = function () {
    //ming_app: 'b88af5f35d5e00c948ae2bda2d867590',
    function init() {
        //$.getScript("http://vietid.net/login/Checksession");
        //try {
        //console.log('a');
        MINGID_NOTIF_FUNC.initNotif("b88af5f35d5e00c948ae2bda2d867590");
        //} catch (e) { }
    }
    function logout() {
        var i = new Image();
        i.src = "http://vietid.net/OauthServerV2/logout?app_key=b88af5f35d5e00c948ae2bda2d867590&call_back=" + window.location.href;
        setSignOutVietIdStatus(true);
        $("#idNameMing").html("");
        $("#divLoginInfo").hide();
        $("#divLogin").show();

        setTimeout(function () {
            reloadIFrame();
        }, 1000);
    }

    return {
        init: function () { return init(); },
        logout: function () { return logout(); }
    };
} (jQuery);

function openIframe() {
    if ($('#basic-modal-content').length > 0) $('#basic-modal-content').remove();
    var popupbox = $('<div />').attr('id', 'basic-modal-content').appendTo($('body'));
    var loginIframe = $("<iframe scroll=\"no\"  style='background: none repeat scroll 0 0 transparent;border: 0 none;height: 490px;width: 100%;'/>").attr("src", '/Pages/VietID/Login.aspx').appendTo(popupbox);
    popupbox.modal();
    return false;
}


$(document).ready(function () {
    MingNotify.init();
});


/***************************************
= Ming
-------------------------------------- */
var URL = "http://soha.vn/";
var URL_LOGOUT = String.format("{0}Pages/VietID/LogOut.aspx", URL);

function reload() {
    document.location = document.location.href.replace("#", "");
}
function openLogin() {
    removeSignOutVietIdStatus();
    if ($(".login-overlay").length > 0)
        $(".login-overlay").remove();
    var width = $(document).width();
    var height = $(document).height();

    var overlay = $("<div />").addClass("login-overlay").css("width", width).css("height", height).appendTo("body");
    var box = $("<div />").addClass("login-box").appendTo("body").center(false);
    var close = $("<a />").addClass("close-button").attr("href", "#").bind("click", function () {
        closeLoginBox();
        return false;
    }).appendTo(box);
    var iframe = $("<iframe />").attr("src", "/Pages/VietID/Login.aspx").appendTo(box);
}

function openLogout() {
    setSignOutVietIdStatus(true);
    var url = "http://vietid.net/OauthServerV2/logout?app_key=b88af5f35d5e00c948ae2bda2d867590&call_back=" + URL + "Pages/VietID/LogOut.aspx?returnUrl=" + window.location.href;
    //window.location.href = "http://vietid.net/login/LogoutService?callback=" + encodeURIComponent(URL_LOGOUT);
    //if ($('#basic-modal-content').length > 0) $('#basic-modal-content').remove();
    //    var popupbox = $('<div />').attr('id', 'basic-modal-content').appendTo($('body'));
    //    var loginIframe = $("<iframe scroll=\"no\"  style='background: none repeat scroll 0 0 transparent;border: 0 none;height: 490px;width: 100%;'/>").attr("src", url).appendTo(popupbox);
    //    popupbox.modal();
    //    return false;

    if ($(".login-overlay").length > 0)
        $(".login-overlay").remove();
    var width = $(document).width();
    var height = $(document).height();

    var overlay = $("<div />").addClass("login-overlay").css("width", width).css("height", height).appendTo("body");
    var box = $("<div />").addClass("login-box").appendTo("body").center(false);
    var close = $("<a />").addClass("close-button").attr("href", "#").bind("click", function () {
        closeLoginBox();
        return false;
    }).appendTo(box);
    var iframe = $("<iframe />").attr({ "src": url }).appendTo(box);
    //box.load(url);
}


function openWindow(url, width, height) {
    var newwindow = window.open(url, 'name', 'width=' + width + ',height=' + height + ',scrollbars=1');
    if (window.focus) { newwindow.focus(); }
    return newwindow;
}

function closeLoginBox() {
    var overlay = $(".login-overlay");
    var box = $(".login-box");
    overlay.fadeOut(500, function () { overlay.remove(); });
    box.fadeOut(500, function () { box.remove(); });
}

function MingReload() {
    document.location.reload();
}

function setCookie(c_name, value, exdays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
    document.cookie = c_name + "=" + c_value;
}

function getCookie(c_name) {
    var c_value = document.cookie;
    var c_start = c_value.indexOf(" " + c_name + "=");
    if (c_start == -1) {
        c_start = c_value.indexOf(c_name + "=");
    }
    if (c_start == -1) {
        c_value = null;
    }
    else {
        c_start = c_value.indexOf("=", c_start) + 1;
        var c_end = c_value.indexOf(";", c_start);
        if (c_end == -1) {
            c_end = c_value.length;
        }
        c_value = unescape(c_value.substring(c_start, c_end));
    }
    return c_value;
}

function CheckSession() {
    //console.log(getSignOutVietIdStatus());
    if (getSignOutVietIdStatus() == null || getSignOutVietIdStatus() == "" || getSignOutVietIdStatus().toLowerCase() == "false")
        $.getScript("http://vietid.net/login/Checksession");
    //    $.ajax({
    //        url: "/Services/VietID/VietIdHandler.ashx", data: { 'command': 'checksession' },
    //        type: 'get',
    //        async: false,
    //        dataType: 'json',
    //        success: function (data) {
    //            userinfo(data);
    //        }
    //    });
}

function userinfo(data) {

    if (data == 'null' || data == '') {
        //neu session tren trang null kiem tra session tren ming
        $.getScript("http://vietid.net/login/Checksession");

        //       var mingAuthCallBack = function (data1) {

        //            if (data1 != 'null') {
        //                //co session tren ming se tu dong login
        //                var rels = eval('(' + data1 + ')');

        //                strQuery = "username=" + rels.username + "&email=" + rels.email + "&full_name=" + rels.full_name + "&id=" + rels.id + "&avatar=" + rels.avatar;
        //                $.ajax({
        //                    url: "/Pages/VietID/DoLogin.aspx?" + strQuery,
        //                    type: 'get',
        //                    async: false,
        //                    dataType: 'json',
        //                    success: function (result) {
        //                        if (result != null || result != '') {
        //                            setLogin(data1);
        //                        }
        //                    }
        //                });
        //            }
        //        };
    }
    else {
        setLogin(data);
    }
}


function JSONscriptRequest(fullUrl) {
    // REST request path
    this.fullUrl = fullUrl;
    // Keep IE from caching requests
    this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
    // Get the DOM location to put the script tag
    this.headLoc = document.getElementsByTagName("head").item(0);
    // Generate a unique script tag id
    this.scriptId = 'YJscriptId' + JSONscriptRequest.scriptCounter++;
}

// Static script ID counter
JSONscriptRequest.scriptCounter = 1;

// buildScriptTag method
//
JSONscriptRequest.prototype.buildScriptTag = function () {

    // Create the script tag
    this.scriptObj = document.createElement("script");

    // Add script object attributes
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("src", this.fullUrl); //+ this.noCacheIE
    this.scriptObj.setAttribute("id", this.scriptId);
};

// removeScriptTag method
//
JSONscriptRequest.prototype.removeScriptTag = function () {
    // Destroy the script tag
    this.headLoc.removeChild(this.scriptObj);
};

// addScriptTag method
//
JSONscriptRequest.prototype.addScriptTag = function () {
    // Create the script tag
    this.headLoc.appendChild(this.scriptObj);
};


function setLogin(data) {
    $('#divLoginInfo').show();
    $('#divLogin').hide();
    var rels = eval('(' + data + ')');
    $('#idNameMing').html(rels.username);

    var strQuery1 = "http://live.kenh14.vn/service.ashx?username=" + rels.username + "&email=" + rels.email + "&avatar=&birthday=&full_name=" + rels.full_name + "&gender=" + ((rels.gender == 1) ? "true" : "false") + "&catid=6";
    var sl = new JSONscriptRequest(strQuery1);
    sl.buildScriptTag();
    sl.addScriptTag();
}

function False() { }
function True() { }


